home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / posixpath.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  11KB  |  396 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Common operations on Posix pathnames.
  5.  
  6. Instead of importing this module directly, import os and refer to
  7. this module as os.path.  The "os.path" name is an alias for this
  8. module on Posix systems; on other systems (e.g. Mac, Windows),
  9. os.path provides the same operations in a manner specific to that
  10. platform, and is an alias to another module (e.g. macpath, ntpath).
  11.  
  12. Some of this can actually be useful on non-Posix systems too, e.g.
  13. for manipulation of the pathname component of URLs.
  14. '''
  15. import os
  16. import stat
  17. import genericpath
  18. import warnings
  19. from genericpath import *
  20. __all__ = [
  21.     'normcase',
  22.     'isabs',
  23.     'join',
  24.     'splitdrive',
  25.     'split',
  26.     'splitext',
  27.     'basename',
  28.     'dirname',
  29.     'commonprefix',
  30.     'getsize',
  31.     'getmtime',
  32.     'getatime',
  33.     'getctime',
  34.     'islink',
  35.     'exists',
  36.     'lexists',
  37.     'isdir',
  38.     'isfile',
  39.     'ismount',
  40.     'walk',
  41.     'expanduser',
  42.     'expandvars',
  43.     'normpath',
  44.     'abspath',
  45.     'samefile',
  46.     'sameopenfile',
  47.     'samestat',
  48.     'curdir',
  49.     'pardir',
  50.     'sep',
  51.     'pathsep',
  52.     'defpath',
  53.     'altsep',
  54.     'extsep',
  55.     'devnull',
  56.     'realpath',
  57.     'supports_unicode_filenames',
  58.     'relpath']
  59. curdir = '.'
  60. pardir = '..'
  61. extsep = '.'
  62. sep = '/'
  63. pathsep = ':'
  64. defpath = ':/bin:/usr/bin'
  65. altsep = None
  66. devnull = '/dev/null'
  67.  
  68. def normcase(s):
  69.     '''Normalize case of pathname.  Has no effect under Posix'''
  70.     return s
  71.  
  72.  
  73. def isabs(s):
  74.     '''Test whether a path is absolute'''
  75.     return s.startswith('/')
  76.  
  77.  
  78. def join(a, *p):
  79.     """Join two or more pathname components, inserting '/' as needed.
  80.     If any component is an absolute path, all previous path components
  81.     will be discarded."""
  82.     path = a
  83.     for b in p:
  84.         if b.startswith('/'):
  85.             path = b
  86.             continue
  87.         if path == '' or path.endswith('/'):
  88.             path += b
  89.             continue
  90.         path += '/' + b
  91.     
  92.     return path
  93.  
  94.  
  95. def split(p):
  96.     '''Split a pathname.  Returns tuple "(head, tail)" where "tail" is
  97.     everything after the final slash.  Either part may be empty.'''
  98.     i = p.rfind('/') + 1
  99.     head = p[:i]
  100.     tail = p[i:]
  101.     if head and head != '/' * len(head):
  102.         head = head.rstrip('/')
  103.     
  104.     return (head, tail)
  105.  
  106.  
  107. def splitext(p):
  108.     return genericpath._splitext(p, sep, altsep, extsep)
  109.  
  110. splitext.__doc__ = genericpath._splitext.__doc__
  111.  
  112. def splitdrive(p):
  113.     '''Split a pathname into drive and path. On Posix, drive is always
  114.     empty.'''
  115.     return ('', p)
  116.  
  117.  
  118. def basename(p):
  119.     '''Returns the final component of a pathname'''
  120.     i = p.rfind('/') + 1
  121.     return p[i:]
  122.  
  123.  
  124. def dirname(p):
  125.     '''Returns the directory component of a pathname'''
  126.     i = p.rfind('/') + 1
  127.     head = p[:i]
  128.     if head and head != '/' * len(head):
  129.         head = head.rstrip('/')
  130.     
  131.     return head
  132.  
  133.  
  134. def islink(path):
  135.     '''Test whether a path is a symbolic link'''
  136.     
  137.     try:
  138.         st = os.lstat(path)
  139.     except (os.error, AttributeError):
  140.         return False
  141.  
  142.     return stat.S_ISLNK(st.st_mode)
  143.  
  144.  
  145. def lexists(path):
  146.     '''Test whether a path exists.  Returns True for broken symbolic links'''
  147.     
  148.     try:
  149.         st = os.lstat(path)
  150.     except os.error:
  151.         return False
  152.  
  153.     return True
  154.  
  155.  
  156. def samefile(f1, f2):
  157.     '''Test whether two pathnames reference the same actual file'''
  158.     s1 = os.stat(f1)
  159.     s2 = os.stat(f2)
  160.     return samestat(s1, s2)
  161.  
  162.  
  163. def sameopenfile(fp1, fp2):
  164.     '''Test whether two open file objects reference the same file'''
  165.     s1 = os.fstat(fp1)
  166.     s2 = os.fstat(fp2)
  167.     return samestat(s1, s2)
  168.  
  169.  
  170. def samestat(s1, s2):
  171.     '''Test whether two stat buffers reference the same file'''
  172.     if s1.st_ino == s2.st_ino:
  173.         pass
  174.     return s1.st_dev == s2.st_dev
  175.  
  176.  
  177. def ismount(path):
  178.     '''Test whether a path is a mount point'''
  179.     
  180.     try:
  181.         s1 = os.lstat(path)
  182.         s2 = os.lstat(join(path, '..'))
  183.     except os.error:
  184.         return False
  185.  
  186.     dev1 = s1.st_dev
  187.     dev2 = s2.st_dev
  188.     if dev1 != dev2:
  189.         return True
  190.     ino1 = s1.st_ino
  191.     ino2 = s2.st_ino
  192.     if ino1 == ino2:
  193.         return True
  194.     return False
  195.  
  196.  
  197. def walk(top, func, arg):
  198.     """Directory tree walk with callback function.
  199.  
  200.     For each directory in the directory tree rooted at top (including top
  201.     itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
  202.     dirname is the name of the directory, and fnames a list of the names of
  203.     the files and subdirectories in dirname (excluding '.' and '..').  func
  204.     may modify the fnames list in-place (e.g. via del or slice assignment),
  205.     and walk will only recurse into the subdirectories whose names remain in
  206.     fnames; this can be used to implement a filter, or to impose a specific
  207.     order of visiting.  No semantics are defined for, or required of, arg,
  208.     beyond that arg is always passed to func.  It can be used, e.g., to pass
  209.     a filename pattern, or a mutable object designed to accumulate
  210.     statistics.  Passing None for arg is common."""
  211.     warnings.warnpy3k('In 3.x, os.path.walk is removed in favor of os.walk.', stacklevel = 2)
  212.     
  213.     try:
  214.         names = os.listdir(top)
  215.     except os.error:
  216.         return None
  217.  
  218.     func(arg, top, names)
  219.     for name in names:
  220.         name = join(top, name)
  221.         
  222.         try:
  223.             st = os.lstat(name)
  224.         except os.error:
  225.             continue
  226.  
  227.         if stat.S_ISDIR(st.st_mode):
  228.             walk(name, func, arg)
  229.             continue
  230.     
  231.  
  232.  
  233. def expanduser(path):
  234.     '''Expand ~ and ~user constructions.  If user or $HOME is unknown,
  235.     do nothing.'''
  236.     if not path.startswith('~'):
  237.         return path
  238.     i = path.find('/', 1)
  239.     if i < 0:
  240.         i = len(path)
  241.     
  242.     if i == 1:
  243.         if 'HOME' not in os.environ:
  244.             import pwd as pwd
  245.             userhome = pwd.getpwuid(os.getuid()).pw_dir
  246.         else:
  247.             userhome = os.environ['HOME']
  248.     else:
  249.         import pwd as pwd
  250.         
  251.         try:
  252.             pwent = pwd.getpwnam(path[1:i])
  253.         except KeyError:
  254.             return path
  255.  
  256.         userhome = pwent.pw_dir
  257.     if not userhome.rstrip('/'):
  258.         pass
  259.     userhome = userhome
  260.     return userhome + path[i:]
  261.  
  262. _varprog = None
  263.  
  264. def expandvars(path):
  265.     '''Expand shell variables of form $var and ${var}.  Unknown variables
  266.     are left unchanged.'''
  267.     global _varprog
  268.     if '$' not in path:
  269.         return path
  270.     if not _varprog:
  271.         import re as re
  272.         _varprog = re.compile('\\$(\\w+|\\{[^}]*\\})')
  273.     
  274.     i = 0
  275.     while True:
  276.         m = _varprog.search(path, i)
  277.         if not m:
  278.             break
  279.         
  280.         (i, j) = m.span(0)
  281.         name = m.group(1)
  282.         if name.startswith('{') and name.endswith('}'):
  283.             name = name[1:-1]
  284.         
  285.         if name in os.environ:
  286.             tail = path[j:]
  287.             path = path[:i] + os.environ[name]
  288.             i = len(path)
  289.             path += tail
  290.             continue
  291.         i = j
  292.     return path
  293.  
  294.  
  295. def normpath(path):
  296.     '''Normalize path, eliminating double slashes, etc.'''
  297.     if path == '':
  298.         return '.'
  299.     initial_slashes = path.startswith('/')
  300.     if initial_slashes and path.startswith('//') and not path.startswith('///'):
  301.         initial_slashes = 2
  302.     
  303.     comps = path.split('/')
  304.     new_comps = []
  305.     for comp in comps:
  306.         if comp in ('', '.'):
  307.             continue
  308.         
  309.         if not comp != '..':
  310.             if (not initial_slashes or not new_comps or new_comps) and new_comps[-1] == '..':
  311.                 new_comps.append(comp)
  312.                 continue
  313.         if new_comps:
  314.             new_comps.pop()
  315.             continue
  316.     
  317.     comps = new_comps
  318.     path = '/'.join(comps)
  319.     if initial_slashes:
  320.         path = '/' * initial_slashes + path
  321.     
  322.     if not path:
  323.         pass
  324.     return '.'
  325.  
  326.  
  327. def abspath(path):
  328.     '''Return an absolute path.'''
  329.     if not isabs(path):
  330.         path = join(os.getcwd(), path)
  331.     
  332.     return normpath(path)
  333.  
  334.  
  335. def realpath(filename):
  336.     '''Return the canonical path of the specified filename, eliminating any
  337. symbolic links encountered in the path.'''
  338.     if isabs(filename):
  339.         bits = [
  340.             '/'] + filename.split('/')[1:]
  341.     else:
  342.         bits = [
  343.             ''] + filename.split('/')
  344.     for i in range(2, len(bits) + 1):
  345.         component = join(*bits[0:i])
  346.         if islink(component):
  347.             resolved = _resolve_link(component)
  348.             if resolved is None:
  349.                 return abspath(join(*[
  350.                     component] + bits[i:]))
  351.             newpath = join(*[
  352.                 resolved] + bits[i:])
  353.             return realpath(newpath)
  354.         islink(component)
  355.     
  356.     return abspath(filename)
  357.  
  358.  
  359. def _resolve_link(path):
  360.     """Internal helper function.  Takes a path and follows symlinks
  361.     until we either arrive at something that isn't a symlink, or
  362.     encounter a path we've seen before (meaning that there's a loop).
  363.     """
  364.     paths_seen = []
  365.     while islink(path):
  366.         if path in paths_seen:
  367.             return None
  368.         paths_seen.append(path)
  369.         resolved = os.readlink(path)
  370.         if not isabs(resolved):
  371.             dir = dirname(path)
  372.             path = normpath(join(dir, resolved))
  373.             continue
  374.         path in paths_seen
  375.         path = normpath(resolved)
  376.     return path
  377.  
  378. supports_unicode_filenames = False
  379.  
  380. def relpath(path, start = curdir):
  381.     '''Return a relative version of a path'''
  382.     if not path:
  383.         raise ValueError('no path specified')
  384.     path
  385.     start_list = abspath(start).split(sep)
  386.     path_list = abspath(path).split(sep)
  387.     i = len(commonprefix([
  388.         start_list,
  389.         path_list]))
  390.     rel_list = [
  391.         pardir] * (len(start_list) - i) + path_list[i:]
  392.     if not rel_list:
  393.         return curdir
  394.     return join(*rel_list)
  395.  
  396.